package com.eeepay.eeepay_v2.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;

import com.eeepay.eeepay_v2.R;
import com.eeepay.v2_library.view.LabelEditText;
import com.eeepay.v2_library.view.LeftRightText;

import java.util.HashMap;

import cn.eeepay.api.grpc.nano.MerAddModular;

/**
 * Created by Ching on 2016/4/14.
 */
public class LimitAdapter extends BaseAdapter {

    private Context mContext;
    private MerAddModular.ServiceQuota[] datas;
    private Button btn;
    //定义hashMap 用来存放之前创建的每一项item
    HashMap<Integer, View> lmap = new HashMap<Integer, View>();

    public LimitAdapter(Context mContext, Button btn) {
        this.mContext = mContext;
        this.btn = btn;
    }

    public void addAll(MerAddModular.ServiceQuota[] datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public MerAddModular.ServiceQuota[] getDatas() {
        return datas;
    }

    @Override
    public int getCount() {
        return datas.length;
    }

    @Override
    public MerAddModular.ServiceQuota getItem(int position) {
        return datas[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        MerAddModular.ServiceQuota limit = datas[position];
        if (lmap.get(position) == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_service_limit_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
            lmap.put(position, convertView);
        } else {
            convertView = lmap.get(position);
            holder = (ViewHolder) convertView.getTag();
        }
//        if (convertView != null) {
//            holder = (ViewHolder) convertView.getTag();
//        } else {
//            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_service_limit_list, parent, false);
//            holder = new ViewHolder();
//            holder.serviceName = (LeftRightText) convertView.findViewById(R.id.lrt_limit_name);
//            holder.cardType = (LeftRightText) convertView.findViewById(R.id.lrt_limit_card_type);
//            holder.holidaysMark = (LeftRightText) convertView.findViewById(R.id.lrt_limit_time);
//            holder.singleCountAmount = (LabelEditText) convertView.findViewById(R.id.let_limit_single_largest);
//            holder.singleDayAmount = (LabelEditText) convertView.findViewById(R.id.let_limit_single_day_largest);
//            holder.singleDaycardAmount = (LabelEditText) convertView.findViewById(R.id.let_limit_day_card_largest);
//            holder.singleDaycardCount = (LabelEditText) convertView.findViewById(R.id.let_limit_day_card_largest_count);
//            convertView.setTag(holder);
//        }
        holder.serviceName.setRightText(limit.serviceName);
        int type = Integer.parseInt(limit.cardType);
        String cardType = null;
        switch (type) {
            case 0:
                cardType = "不限";
                break;
            case 1:
                cardType = "信用卡";
                break;
            case 2:
                cardType = "储蓄卡";
                break;
        }
        holder.cardType.setRightText(cardType);
        int time = Integer.parseInt(limit.holidaysMark);
        String serviceTime = null;
        switch (time) {
            case 0:
                serviceTime = "不限";
                break;
            case 1:
                serviceTime = "工作日";
                break;
            case 2:
                serviceTime = "节假日";
                break;
        }
        holder.holidaysMark.setRightText(serviceTime);
        holder.singleMinAmount.setText(limit.singleMinAmount);
        holder.singleCountAmount.setText(limit.singleCountAmount);
        holder.singleDayAmount.setText(limit.singleDayAmount);
        holder.singleDaycardAmount.setText(limit.singleDaycardAmount);
        holder.singleDaycardCount.setText(limit.singleDaycardCount);
//        holder.singleCountAmount.setEditContent(limit.singleCountAmount);
//        holder.singleDayAmount.setEditContent(limit.singleDayAmount);
//        holder.singleDaycardAmount.setEditContent(limit.singleDaycardAmount);
//        holder.singleDaycardCount.setEditContent(limit.singleDaycardCount);

        holder.singleMinAmount.setTag(position);
        holder.singleCountAmount.setTag(position);
        holder.singleDayAmount.setTag(position);
        holder.singleDaycardAmount.setTag(position);
        holder.singleDaycardCount.setTag(position);
//        holder.singleCountAmount.setEditContent(FormatUtil.DecimaNum(limit.singleCountAmount));
//        holder.singleDayAmount.setEditContent(FormatUtil.DecimaNum(limit.singleDayAmount));
//        holder.singleDaycardAmount.setEditContent(FormatUtil.DecimaNum(limit.singleDaycardAmount));
//        holder.singleDaycardCount.setEditContent(FormatUtil.DecimaNum(limit.singleDaycardCount));

        if ("1".equals(limit.fixedQuota)) {//限额不可修改
            holder.singleMinAmount.setEnabled(false);
            holder.singleCountAmount.setEnabled(false);
            holder.singleDayAmount.setEnabled(false);
            holder.singleDaycardAmount.setEnabled(false);
            holder.singleDaycardCount.setEnabled(false);

            holder.singleMinAmount.setBackground(null);
            holder.singleCountAmount.setBackground(null);
            holder.singleDayAmount.setBackground(null);
            holder.singleDaycardAmount.setBackground(null);
            holder.singleDaycardCount.setBackground(null);
            holder.singleMinAmount.setGravity(Gravity.RIGHT);
            holder.singleCountAmount.setGravity(Gravity.RIGHT);
            holder.singleDayAmount.setGravity(Gravity.RIGHT);
            holder.singleDaycardAmount.setGravity(Gravity.RIGHT);
            holder.singleDaycardCount.setGravity(Gravity.RIGHT);
        } else {
            class EditListener implements TextWatcher {
                private EditText editText;

                //                public EditListener(LabelEditText editText) {
//                    this.editText = editText;
//                }
                public EditListener(EditText editText) {
                    this.editText = editText;
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                @Override
                public void afterTextChanged(Editable s) {
//                if (!TextUtils.isEmpty(s.toString())) {
                    int position = (int) editText.getTag();
                    switch (editText.getId()) {
                        case R.id.et_limit_minimum_amount:
                            datas[position].singleMinAmount = s.toString();
                            break;
                        case R.id.et_limit_single_largest:
                            datas[position].singleCountAmount = s.toString();
//                            limit.singleCountAmount = s.toString();
                            break;
                        case R.id.et_limit_single_day_largest:
                            datas[position].singleDayAmount = s.toString();
//                            limit.singleDayAmount = s.toString();
                            break;
                        case R.id.et_limit_day_card_largest:
                            datas[position].singleDaycardAmount = s.toString();
//                            limit.singleDaycardAmount = s.toString();
                            break;
                        case R.id.et_limit_day_card_largest_count:
                            datas[position].singleDaycardCount = s.toString();
//                            limit.singleDaycardCount = s.toString();
                            break;
                    }
                }
            }
//            LabelEditText[] arr = new LabelEditText[]{holder.singleCountAmount, holder.singleDayAmount,
//                    holder.singleDaycardAmount, holder.singleDaycardCount};
//            LabelEditText let;
            EditText[] arr = new EditText[]{holder.singleMinAmount, holder.singleCountAmount, holder.singleDayAmount,
                    holder.singleDaycardAmount, holder.singleDaycardCount};
            EditText editText;
            for (int i = 0; i < arr.length; i++) {
                editText = arr[i];
                editText.setTag(position);
//                editText.setMinimumWidth(2);
//                if ("1".equals(limit.fixedQuota)) {
////                    editText.setEnabled(false);
////                    editText.setBackgroundColor(Color.BLUE);
////                    editText.setBackgroundResource(0);
////                    editText.setBackground(null);
//                } else {
                editText.addTextChangedListener(new EditListener(arr[i]));
//                }
//                let.getEditText().setBackgroundResource(R.drawable.edittext_bg);
//                let.getEditText().setMinimumWidth(2);
//                let.getEditText().addTextChangedListener(new EditListener(arr[i]));
            }
        }
        return convertView;
    }

    class ViewHolder {
        public LeftRightText serviceName, cardType, holidaysMark;
        public EditText singleMinAmount, singleCountAmount, singleDayAmount, singleDaycardAmount, singleDaycardCount;

        public ViewHolder(View convertView) {
            serviceName = (LeftRightText) convertView.findViewById(R.id.lrt_limit_name);
            cardType = (LeftRightText) convertView.findViewById(R.id.lrt_limit_card_type);
            holidaysMark = (LeftRightText) convertView.findViewById(R.id.lrt_limit_time);

            singleMinAmount = (EditText) convertView.findViewById(R.id.et_limit_minimum_amount);
            singleCountAmount = (EditText) convertView.findViewById(R.id.et_limit_single_largest);
            singleDayAmount = (EditText) convertView.findViewById(R.id.et_limit_single_day_largest);
            singleDaycardAmount = (EditText) convertView.findViewById(R.id.et_limit_day_card_largest);
            singleDaycardCount = (EditText) convertView.findViewById(R.id.et_limit_day_card_largest_count);
        }

    }

}
